import { Component ,OnInit} from '@angular/core';
import { Title } from '@angular/platform-browser';
import { Config } from './config'
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css',]
})
export class AppComponent implements OnInit {
  constructor(private titleService: Title, private config: Config, private router: Router, private activatedRouter: ActivatedRoute) {

  }

  ngOnInit() {
    this.titleService.setTitle(this.config.title);

    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .map(() => this.activatedRouter)
      .map((router) => {
        while (router.firstChild) {
          router = router.firstChild
        }
        return router;
      })
      .filter(route => route.outlet=='primary')
      .mergeMap((route) => route.data)
      .subscribe((event) => this.titleService.setTitle(this.config.title + ' - ' + event.title));
  }
}
